#!/bin/sh
main()
{
. $DOCUMENT_ROOT/apps/home/header.tpl.sh
header_html

cat <<EOF
		<div class="pure-g">
			<div class="l-box-lrg pure-u-1 pure-u-md-1-2">
				<h3>端口映射管理</h3>
	<table class="pure-table pure-table-bordered">
		<thead>
			<tr>
			<th>Mark</th>
			<th>Wan_netzone</th>
			<th>端口</th>
			<th>Proto</th>
			<th>Lan_netzone</th>
			<th>LanIP</th>
			<th>LanPort</th>
			</tr>
		</thead>
		<tbody>
<script>
function del_rule(rule)
{
if (confirm("确定要删除 " + rule + " ?")) {
    var data = "app=portforwarding&action=post_del_rule&rule=" + rule;
    var url = 'index.cgi';
    Ha.common.ajax(url, 'json', data, 'post', 'ajax-fluid');
	setTimeout("window.location.reload();", 4000);
  }
}
</script>
EOF
wan_zone_list=`$DOCUMENT_ROOT/apps/netzone/netzone.sbin list_all_wanzone`
lan_zone_list=`$DOCUMENT_ROOT/apps/netzone/netzone.sbin list_all_lanzone`
for rule in `uci -X show firewall | grep 'tag=PORTFORWARD$' | cut -d '.' -f2`
do
dest= ; dest_ip= ; dest_port= ; name= ; proto= ; src= ; src_dport=
eval `uci show firewall.${rule} | cut -d '.' -f3- | grep -v "^$" | sed -e 's/=/="/g' -e 's/$/"/g'`
cat <<EOF
<script>
\$(function(){
  \$('#content_rule_${rule}').on('submit', function(e){
    e.preventDefault();
    var data = "app=portforwarding&action=post_add_portforwarding&rule=${rule}&"+\$(this).serialize();
    var url = 'index.cgi';
    Ha.common.ajax(url, 'json', data, 'post', 'content_rule_${rule}');
  });
});
</script>
<form class="pure-form" id="content_rule_${rule}">
    <tr>
		<td>
        <input type="text" placeholder="注释" name="name" value="${name}">
		</td>
		<td>
			<select name="wan_zone">
EOF

for wan_zone in $wan_zone_list
do
cat <<EOF
				<option value="${wan_zone}" `[ "${src}" = "${wan_zone}" ] && echo selected`>${wan_zone}</option>
EOF
done
cat <<EOF
			</select>
		</td>
		<td>
<div class="pure-control-group">
	<label for="name">源端口</label>
	<input type="text" placeholder="${src_dport}(不可为空)" name="src_dport" value="${src_dport}">
</div>
<div class="pure-control-group">
	<label for="name">目标端口</label>
	<input type="text" placeholder="${dest_port}(可为空)" name="dest_port" value="${dest_port}">
</div>
		</td>
		<td>
        <input type="text" placeholder="[tcp|udp|tcpudp|icmp]" name="proto" value="${proto}">
		</td>
		<td>
			<select name="lan_zone">
EOF

for lan_zone in $lan_zone_list
do
cat <<EOF
				<option value="${lan_zone}" `[ "${dest}" = "${lan_zone}" ] && echo selected`>${lan_zone}</option>
EOF
done
cat <<EOF
			</select>
		</td>
		<td>
        <input type="text" placeholder="${dest_ip}" name="dest_ip" value="${dest_ip}">
		</td>
		<td>
        <button type="submit" class="pure-button pure-button-primary">保存</button>
        <button onclick="del_rule('${rule}')" type="submit" class="pure-button button-error">删除</button>
		</td>
    </tr>
</form>
EOF
done
cat <<EOF
<script>
\$(function(){
  \$('#content_rule_new').on('submit', function(e){
    e.preventDefault();
    var data = "app=portforwarding&action=post_add_portforwarding&"+\$(this).serialize();
    var url = 'index.cgi';
    Ha.common.ajax(url, 'json', data, 'post', 'content_rule_new');
	setTimeout("window.location.reload();", 4000);
  });
});
</script>
<form class="pure-form" id="content_rule_new">
    <tr>
		<td>
		<input type="text" placeholder="注释(可为空)" name="name">
		</td>
		<td>
			<select name="wan_zone">
EOF

for wan_zone in $wan_zone_list
do
cat <<EOF
				<option value="${wan_zone}" `[ "${src}" = "${wan_zone}" ] && echo selected`>${wan_zone}</option>
EOF
done
cat <<EOF
			</select>
		</td>
		<td>
<div class="pure-control-group">
	<label for="name">源端口</label>
	<input type="text" placeholder="${src_dport}(不可为空)" name="src_dport">
</div>
<div class="pure-control-group">
	<label for="name">目标端口</label>
	<input type="text" placeholder="${dest_port}(可为空)" name="dest_port">
</div>
		</td>
		<td>
        <input type="text" placeholder="[tcp|udp|tcpudp|icmp]" name="proto" value="tcp">
		</td>
		<td>
			<select name="lan_zone">
EOF

for lan_zone in $lan_zone_list
do
cat <<EOF
				<option value="${lan_zone}" `[ "${dest}" = "${lan_zone}" ] && echo selected`>${lan_zone}</option>
EOF
done
cat <<EOF
			</select>
		</td>
		<td>
        <input type="text" placeholder="10.10.10.3" name="dest_ip">
		</td>
		<td>
        <button type="submit" class="pure-button pure-button-primary">新增</button>
		</td>
    </tr>
</form>
		</tbody>
	</table>

			</div>
		</div>
EOF
footer_html
}
post_add_portforwarding()
{
$DOCUMENT_ROOT/apps/$FORM_app/portforwarding.sbin post_add_portforwarding
}
post_del_rule()
{
work()
{
uci set firewall.${FORM_rule}=""
uci commit firewall
/etc/init.d/firewall restart 2>&1 >/dev/null &
}
work 2>&1 >/dev/null
echo "端口删除成功!!" | main.sbin output_json 0
}
get_portforwarding_rules()
{
printf "Content-Type: text/html; charset=utf-8\r\n\r\n"
$DOCUMENT_ROOT/apps/$FORM_app/portforwarding.sbin get_portforwarding_rules
}

lang=`main.sbin get_client_lang`
eval `cat $DOCUMENT_ROOT/apps/$FORM_app/i18n/$lang/i18n.conf`
if
[ $is_main_page = 1 ]
then
main
elif [ -n "$FORM_action" ]
then
$FORM_action
fi
